<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    let obj1 = {
      a: {
        b: 1,
      },
    };
    function deepClone(obj) {
      let cloneObj = {};
      for (let key in obj) {
        //遍历
        if (typeof obj[key] === 'object') {
          cloneObj[key] = deepClone(obj[key]); //是对象就再次调用该函数递归
        } else {
          cloneObj[key] = obj[key]; //基本类型的话直接复制值
        }
      }
      return cloneObj;
    }

    let obj2 = deepClone(obj1);
    obj1.a.b = 2;
    console.log(obj2);
  </script>
</html>
